package ink.zhaohui.chapter3;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TraceThreadPoolExecutor extends ThreadPoolExecutor {

    public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        //TODO Auto-generated constructor stub
    }

    @Override
    public void execute(Runnable task){
        super.execute(wrap(task,clientTrace(),Thread.currentThread().getName()));
    }

    @Override
    public  Future<?> submit(Runnable task) {
        // TODO Auto-generated method stub
        return super.submit(wrap(task,clientTrace(),Thread.currentThread().getName()));
    }

    private RuntimeException clientTrace(){
        return new RuntimeException("Client stack trace.");
    }

    private Runnable wrap(final Runnable task,final RuntimeException clientStack,String clientThreadName){
        return new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try{
                    task.run();
                }catch(Exception e){
                    clientStack.addSuppressed(e);
                    throw clientStack;
                }
            }
            
        };
    }


    
}
